package cl.uchile.sketchpad.services;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cl.uchile.sketchpad.persistance.dao.SubjectVersionDao;
import cl.uchile.sketchpad.persistance.dao.SubjectVersionRegistrationDao;
import cl.uchile.sketchpad.persistance.dao.UserDAO;
import cl.uchile.sketchpad.persistance.model.SubjectVersion;
import cl.uchile.sketchpad.persistance.model.SubjectVersionRegistration;
import cl.uchile.sketchpad.persistance.model.User;

@Service
public class SubjectVersionRegistrationServiceImpl implements SubjectVersionRegistrationService {
	
	private static final Logger logger = LoggerFactory.getLogger(SubjectVersionRegistrationServiceImpl.class);
	
	@Autowired
	private SubjectVersionRegistrationDao subjectVersionRegistrationDao;
	
	@Autowired
	private UserDAO userDao;
	
	@Autowired
	private SubjectVersionDao subjectVersionDao;

	@Override
	@Transactional(readOnly = true)
	public List<SubjectVersionRegistration> getAll() {
		return this.subjectVersionRegistrationDao.getAll();
	}

	@Override
	@Transactional
	public void persist(SubjectVersionRegistration subjectVersionRegistration) {
		this.subjectVersionRegistrationDao.persist(subjectVersionRegistration);
	}

	@Override
	public void persistMailRegistrations(List<String> mails, SubjectVersion subjectVersion) {
		SubjectVersionRegistration svr = null;
		for ( String mail : mails ) {
			svr = new SubjectVersionRegistration();
			svr.setSubjectVersion(subjectVersion);
			svr.setUserMail(mail);
			this.subjectVersionRegistrationDao.persist(svr);
		}
	}

	@Override
	public HashMap<SubjectVersion, List<User>> assignSubjectVersionUsers() {
		HashMap<SubjectVersion, List<User>> assigned = new HashMap<SubjectVersion, List<User>>(0);
		
		List<SubjectVersionRegistration> svrs = this.subjectVersionRegistrationDao.getAll();
		List<SubjectVersionRegistration> svrsToDelete = new LinkedList<SubjectVersionRegistration>();
		User user = null;
		
		for ( SubjectVersionRegistration svr : svrs ) {
			user = this.userDao.findByMail(svr.getUserMail().toLowerCase());
			if ( user != null ) {
				try {
					if ( !user.getSubjectVersions().contains(svr.getSubjectVersion()) ) {
						user.getSubjectVersions().add(svr.getSubjectVersion());
						this.userDao.persist(user);
						
						List<User> users = assigned.get(svr.getSubjectVersion());
						if ( users == null )
							users = new LinkedList<User>();
						users.add(user);
						assigned.put(svr.getSubjectVersion(), users);
					}
					
					svrsToDelete.add(svr);
					
				} catch (Exception e) {
					logger.error(e.toString());
				}
			}
		}
		
		for ( SubjectVersionRegistration svr : svrsToDelete ) {
			try {
				this.subjectVersionRegistrationDao.remove(svr);
			} catch (Exception e) {
				logger.error(e.toString());;
			}
		}
		
		return assigned;
	}

}
